import * as api from "../../api/mine";
const moment = require("../../utils/moment.min");
var dayjs = require('dayjs');

Page({
    data: {
        increasePerson: 0,
        increaseSaleCount: 0,
        saleCount: 0,
        saleOrder: 0,
        salePerson: 0,
        dateItemList:[{
            label:"昨日",
            value:"yesterday"
        },{
            label:"今日",
            value:"today"
        },{
            label:"近7天",
            value:"near7"
        },{
            label:"近30天",
            value:"near30"
        },{
            label:"本周",
            value:"thisWeek"
        },{
            label:"上周",
            value:"lastWeek"
        },{
            label:"本月",
            value:"thisMonth"
        },{
            label:"上月",
            value:"lastMonth"
        }],
        dateItemIndex:1,
        start:"",
        end:"",
        dataItemOpen:false,
        currentPage:1,
        list:[],
        userLevel:0,
        totalIncreasePerson: 0,
        totalIncreaseSaleCount: 0,
        totalSaleCount: 0,
        totalSalePerson: 0,

        hasNextPage:true,
        total:"",
        userName:"",
        showTipsBox:false,
        fixedHeight:0
    },
    async onLoad(){

        let info = await api.get_user_info();
        let topData = await api.mineCountDataMonth();

        this.setData({
            increasePerson: topData.increasePerson,
            increaseSaleCount: topData.increaseSaleCount,
            saleCount: topData.saleCount,
            saleOrder: topData.saleOrder,
            salePerson: topData.salePerson,
            start:moment().format("YYYY-MM-DD"),
            end:moment().format("YYYY-MM-DD"),
            userLevel:info.userLevel
        })

        this.get_data_list();

        let query = wx.createSelectorQuery();
        query.select('.fixed').boundingClientRect(rect=>{
            let clientHeight = rect.height;
            let clientWidth = rect.width;
            let ratio = 750 / clientWidth;
            let height = clientHeight * ratio;
            this.setData({
                fixedHeight:height
            })
        }).exec();

    },
    get_input_value(e){
        this.setData({
            userName:e.detail.value
        })
    },
    get_data_list(){
        api.mineCountDataPage({
            currentPage:this.data.currentPage,
            pageSize:20,
            userName:this.data.userName,
            countDateStart:this.data.start,
            countDateEnd:this.data.end
        })
            .then((res)=>{

                let pageInfo = res.pageInfo;
                if(pageInfo){
                    this.setData({
                        list:[...this.data.list, ...pageInfo.list],
                        hasNextPage:pageInfo.hasNextPage,
                        total:pageInfo.total,
                        totalIncreasePerson: res.increasePerson,
                        totalIncreaseSaleCount: res.increaseSaleCount,
                        totalSaleCount: res.saleCount,
                        totalSalePerson: res.salePerson
                    })
                }
                else{
                    this.setData({
                        list:[],
                        hasNextPage:false,
                        total:0
                    })
                }
                
            })
    },
    confirm_date_item(e){
        let index = e.detail.value;
        let value = this.data.dateItemList[index].value;
        let start, end;

        if(value == 'yesterday'){
            start = moment().subtract(1, 'days').format("YYYY-MM-DD");
            end = moment().subtract(1, 'days').format("YYYY-MM-DD")
        }

        if(value == 'today'){
            start = moment().format("YYYY-MM-DD");
            end = moment().format("YYYY-MM-DD")
        }

        if(value == 'near7'){
            start = moment().subtract(6, 'days').format("YYYY-MM-DD");
            end = moment().subtract().format("YYYY-MM-DD")
        }

        if(value == 'near30'){
            start = moment().subtract(29, 'days').format("YYYY-MM-DD");
            end = moment().format("YYYY-MM-DD")
        }

        if(value == 'thisWeek'){
            start = moment().weekday(1).format("YYYY-MM-DD");
            end = moment().format("YYYY-MM-DD")
        }

        if(value == 'lastWeek'){
            start = moment().weekday(1).subtract(7, 'days').format("YYYY-MM-DD");
            end = moment().weekday(7).subtract(7, 'days').format("YYYY-MM-DD")
        }

        if(value == 'thisMonth'){
            start = moment().startOf('month').format("YYYY-MM-DD");
            end = moment().format("YYYY-MM-DD");
        }

        if(value == 'lastMonth'){
            start = moment().subtract(1, 'months').startOf('month').format("YYYY-MM-DD");
            end = moment().subtract(1, 'months').endOf('month').format("YYYY-MM-DD");
        }

        this.setData({
            dateItemIndex:index,
            dataItemOpen:false,
            start:start,
            end:end,
            currentPage:1,
            hasNextPage:true,
            list:[]
        });

        this.get_data_list();
    },
    top_picker_confirm(e){
        this.setData({
            start:e.detail[0],
            end:e.detail[1],
            currentPage:1,
            hasNextPage:true,
            list:[]
        });

        this.get_data_list();
    },
    search_user_list(){
        this.setData({
            currentPage:1,
            hasNextPage:true,
            list:[]
        })
        this.get_data_list();
    },
    open_date_item(){
        this.setData({
            dataItemOpen:true
        })
    },
    cancel_date_item(){
        this.setData({
            dataItemOpen:false
        })
    },
    onReachBottom(){
        if(this.data.hasNextPage){
            this.setData({
                currentPage: ++this.data.currentPage
            })
            this.get_data_list();
        }
    },
    open_tips_box(){
        this.setData({
            showTipsBox:!this.data.showTipsBox
        })
    }
})


